package fm.last.time.stats;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.util.Log;

public class RecentTracksRequester extends LastFmRequester {

	
	private static final String FROM_KEY = "from";
	private static final String TO_KEY = "to";
	private static final String PAGE_KEY = "page";
	private static final String USER_KEY = "user";
	private static final String METHOD = "user.getrecenttracks";

	// example http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=rj&api_key=b25b959554ed76058ac220b7b2e0a026
	public RecentTracksRequester() {
		super(METHOD);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected String doInBackground(String... params) {
		String userName = params[0];
		String page = params[1];
		String to = params[2];
		String from = params[3];
		String apiKey = params[4];
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put(USER_KEY,userName);
		parameters.put(PAGE_KEY, page);
		parameters.put(TO_KEY, to);
		parameters.put(FROM_KEY, from);
		parameters.put(API_KEY_KEY, apiKey);
		parameters.put(USERNAME_KEY, userName);
		parameters.put(METHOD_KEY, METHOD);
		parameters.put(FORMAT, JSON);
		String data ="";
		try {
			StringBuilder json = new StringBuilder();
            BufferedReader reader = 
                new BufferedReader(
                            new InputStreamReader(getData(parameters)));
            String line = reader.readLine();
            while (line != null){
                json.append(line);
                line = reader.readLine();
            }
			data = json.toString();
            Log.e("lastfmstats", data);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}

}
